$(function(){
   $('#order_grid').delegate('.form-row','click',function(){

     $('#order_grid .form-row .orderlist_item_wrapper>div').removeClass('active');
     $(this).find('.orderlist_item_wrapper div').addClass('active');


   });





});  //页面加载完毕

//hjl  订单列表页 弹窗 邮件验证

function sendOrderEmailToCustomer(oid){


		var email=$('#order_customer_email').val();

		var myReg=/^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/gi;
		if(!myReg.test(email)){
		//验证失败
		$('#advice-required-entry-order_customer_email').show();
		return;
		}else{
		//验证成功
			$('#advice-required-entry-order_customer_email').hide();
			var ajaxurl = "index.php?act=sendOrderemail";
			$.ajax({
				url:ajaxurl,
				data:{"oid":oid,"email":email},
				type:"Post",
				dataType:"json",
				success:function(data){
					if(data.error>0){
						msg('danger',data.message);//处理错误信息;
					}else{
						cancelAddCustomer()
						msg('success','发送邮件成功');
					}
				}
			})
		}
 
  }
  
  //miracle 订单描述显示隐藏
	function coment_order(){
		if($("#order-view-comment").css('display') == 'none'){
			$("#order-view-comment").css('display','block');
		}else{
			$("#order-view-comment").css('display','none');
		}
	}
  
	//miracle 订单描述修改
	function change_comment(oid){
		var content = $('#order-view-comment-content').val();
		var ajaxurl = "index.php?act=change_comment";
			$.ajax({
				url:ajaxurl,
				data:{"oid":oid,"content":content},
				type:"Post",
				dataType:"json",
				success:function(data){
					if(data.error>0){
						msg('danger',data.message);//处理错误信息;
					}else{
						coment_order();
						$("#order_"+oid).click();//重载订单详情
						msg('success','修改备注成功');
					}
				}
			})
	}
	
	//miracle 开发票
	function send_invoice(oid){
		var ajaxurl = "index.php?act=send_invoice";
			$.ajax({
				url:ajaxurl,
				data:{"oid":oid},
				type:"Post",
				dataType:"json",
				success:function(data){
					if(data.error>0){
						msg('danger',data.message);//处理错误信息;
					}else{
						$('.btn-invoice').hide();
						msg('success','操作成功');
					}
				}
			})
	}
	
	
  //hjl  显示持有订单详情
  function previewHoldedOrder(orderId,obj){

   $(obj).addClass('active');
   $('#holded_orders_detail').css('width','39%');
   $('#holded_orders_detail').css('right','0px');
   $('#holded_orders_detail_loader').show();
   $('#holded_orders_detail_loader').hide();

   var time_stamp = orderId;
    $.ajax(
        { 
          url: "idx.php?act=detail_hold",
          cache:false,
          data: {'time_stamp':time_stamp}, 
          dataType: 'json', 
          type: 'post', 
          beforeSend:function(){
                 $('#ajaxload').show();
                 $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
              },
          success: function(result)
          {
            hideBeforeHoldPopup();
            $('#ajaxload').hide();
            $("#ajaxload img").remove();  
            document.getElementById('holded_orders_detail_content').innerHTML = result.content;
          } 
        });
  }

//更新钱箱 yjmp
function update_cash_drawer()
{
  var amount = parseFloat($('#transaction_amount').val());
  var type =  parseInt($('#transaction_type').val());
  var note = $('#transaction_note').val();
  var balance = parseFloat(document.getElementById('crt_balance').innerHTML.substring(1));

  if ( amount > balance && type == 0) 
  {
    msg('danger','余额不足,操作失败');
  }
  else
  {
    $.ajax(
      { 
        url: "idx.php?act=add_cash_drawer",
        cache:false,
        data: {'amount':amount, 'type':type, 'note':note}, 
        dataType: 'json', 
        type: 'post',
        beforeSend:function(){
               $('#ajaxload').show();
               $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
            }, 
        success: function(result)
        {
          hideBeforeHoldPopup();
          $('#ajaxload').hide();
          $("#ajaxload img").remove();
          document.getElementById('cashdrawer_area').innerHTML = result.content;
          $('#cdexport').click(function(){
       
           if($('#transaction_export').hasClass('in')){
           
             $('#transaction_export').removeClass('in');
           }else{
            
             $('#transaction_export').addClass('in');
           }
          });
        } 
      });
  }
}

//改SESSION yjmp
function change_session()
{
  var checkicon = document.getElementsByClassName('checkicon');
  for (var i = 0; i < checkicon.length; i++) 
  {
    if (checkicon[i].className == 'checkicon glyphicon glyphicon-ok haschoose') 
    {
      var session_out = checkicon[i].id;
      $.ajax(
      { 
        url: "idx.php?act=session_out",
        cache:false,
        data: {'session_out':session_out}, 
        dataType: 'json', 
        type: 'post',
        beforeSend:function(){
               $('#ajaxload').show();
               $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
            }, 
        success: function(result)
        {
          hideBeforeHoldPopup();
          $('#ajaxload').hide();
          $("#ajaxload img").remove();
          document.getElementById('content_general').innerHTML = result.content;
          $('#content_general form .box1 div').on('click',function(){
           $('#content_general form .box1 div span').removeClass('glyphicon glyphicon-ok haschoose');
           $(this).find("span").addClass('glyphicon glyphicon-ok haschoose');
          });
          $('#change_session').on('click',function(){change_session()});
        } 
      });
    }
  }
}

//修改用户个人信息  yjmp
function change_profile()
{
  var user_name = $('#username').val();
  var email = $('#emailaddress').val();
  var old_pwd = $('#oldpassword').val();
  var pwd = $('#newpassword').val();
  var cfm_pwd = $('#confirmpassword').val();
  $.ajax(
      { 
        url: "idx.php?act=update_profile",
        cache:false,
        data: {'user_name':user_name,'email':email,'old_pwd':old_pwd,'pwd':pwd,'cfm_pwd':cfm_pwd}, 
        dataType: 'json', 
        type: 'post', 
        beforeSend:function(){
               $('#ajaxload').show();
               $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
            },
        success: function(result)
        {
          hideBeforeHoldPopup();
          $('#ajaxload').hide();
          $("#ajaxload img").remove();
          if (result.error == 1) 
          {
            msg('danger',result.message);
            $("#oldpassword").val('');
            $("#newpassword").val('');
            $("#confirmpassword").val('');
          }
          else
          {
            msg('success',result.message);
          }
        } 
      });
}
//加持  yjmp
function add_to_hold()
  {
    var note = $('#hold_order_descreption').val();
    $.ajax(
      { 
        url: "idx.php?act=add_to_hold",
        cache:false,
        data: {'note':note}, 
        dataType: 'json', 
        type: 'post',
        beforeSend:function(){
               $('#ajaxload').show();
               $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
            }, 
        success: function(result)
        {
          hideBeforeHoldPopup();
          $('#ajaxload').hide();
          $("#ajaxload img").remove();
          document.getElementById('total_number_item').innerHTML = 0; 
          document.getElementById('zongji').innerHTML = 0;      
          document.getElementById('webpos_subtotal_button').innerHTML = 0;              
          document.getElementById('grand_total_webpos').innerHTML = 0;    
          document.getElementById('success_orderGrandTotal').innerHTML = 0;   
          document.getElementById('webpos_cart').innerHTML = result.cart;
          document.getElementById('holding_item_yj').innerHTML = result.hold;
        } 
      });
  }

//hold单放回购物车
function reloadOrderByOrderId(val)
{
  var time = val;
    $.ajax(
      { 
        url: "idx.php?act=reload_cart",
        cache:false,
        data: {'time':time}, 
        dataType: 'json', 
        type: 'post', 
        beforeSend:function(){
           $('#ajaxload').show();
           $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
        },
        success: function(result)
        {
          hideBeforeHoldPopup();
          $('#ajaxload').hide();
          $("#ajaxload img").remove();
          document.getElementById('total_number_item').innerHTML = result.cnum; 
          document.getElementById('zongji').innerHTML = result.zongji;      
          document.getElementById('webpos_subtotal_button').innerHTML = result.zongji;              
          document.getElementById('grand_total_webpos').innerHTML = 0;    
          document.getElementById('success_orderGrandTotal').innerHTML = 0;   
          document.getElementById('webpos_cart').innerHTML = result.cart;
          document.getElementById('holding_item_yj').innerHTML = result.hold;
          yj_logical();
        } 
      });
}

function deleteHoldedOrderByOrderId(val)
{
  var time = val;
    $.ajax(
      { 
        url: "idx.php?act=delete_hold",
        cache:false,
        data: {'time':time}, 
        dataType: 'json', 
        type: 'post', 
        beforeSend:function(){
               $('#ajaxload').show();
               $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
            },
        success: function(result)
        {
          hideBeforeHoldPopup();
          $('#ajaxload').hide();
          $("#ajaxload img").remove();
          document.getElementById('total_number_item').innerHTML = 0; 
          document.getElementById('zongji').innerHTML = 0;      
          document.getElementById('webpos_subtotal_button').innerHTML = 0;              
          document.getElementById('grand_total_webpos').innerHTML = 0;    
          document.getElementById('success_orderGrandTotal').innerHTML = 0;   
          document.getElementById('webpos_cart').innerHTML = result.cart;
          document.getElementById('holding_item_yj').innerHTML = result.hold;
          yj_logical();
        } 
      });
}
//显示hold_order详情

//切换导出菜单显示状态
function show_exp()
{
    var exp = $('#eod_export').attr('class');
    if (exp == 'collapse out') 
    {
      $("#eod_export").attr("class","collapse out in");
    }else{
     $("#eod_export").attr("class","collapse out");
    }
}

function yj_logical()
{
  /*逻辑部分*/
  document.getElementById("settings_area").style.display="none";
  document.getElementById("settings_area").style.zIndex=1;
  document.getElementById("reports_area").style.display="none";
  document.getElementById("reports_area").style.zIndex=1;
  document.getElementById("cashdrawer_area").style.display="none";
  document.getElementById("cashdrawer_area").style.zIndex=1;      
  document.getElementById("orders_area").style.display="none";
  document.getElementById("orders_area").style.zIndex=1;
  document.getElementById("holded_orders_section").style.display="none";
  document.getElementById("holded_orders_section").style.zIndex=1;
  $('#holded_orders_detail').css('right','-100vw');  //关闭持有订单详
  $('.box-logout').hide();
  $('#webpos_medium_overlay').hide();
}

